package com.xuecheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.mapper.MediaFilesMapper;
import com.xuecheng.mapper.MediaProcessHistoryMapper;
import com.xuecheng.mapper.MediaProcessMapper;
import com.xuecheng.po.MediaFiles;
import com.xuecheng.po.MediaProcess;
import com.xuecheng.po.MediaProcessHistory;
import com.xuecheng.service.MediaFileProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class MediaFileProcessServiceImpl implements MediaFileProcessService {
	
	@Resource
	private MediaProcessMapper mediaProcessMapper;
	@Resource
	private MediaProcessHistoryMapper mediaProcessHistoryMapper;
	
	@Resource
	private MediaFilesMapper mediaFilesMapper;
	
	@Override
	public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
		return mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
	}
	
	@Transactional
	@Override
	public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
		
		//查询这个任务
		MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
		if (mediaProcess == null) {
			log.debug("更新任务状态时此任务:{}为空", taskId);
			return;
		}
		LambdaQueryWrapper<MediaProcess> queryWrapperById =
			new LambdaQueryWrapper<MediaProcess>().eq(MediaProcess::getId, taskId);
		if ("3".equals(status)) {
			//任务失败
			MediaProcess mediaProcess_u = new MediaProcess();
			mediaProcess_u.setStatus("3");//处理失败
			mediaProcess_u.setErrormsg(errorMsg);
			mediaProcess_u.setFinishDate(LocalDateTime.now());
			mediaProcessMapper.update(mediaProcess_u, queryWrapperById);
			return;
		}
		//处理成功，更新状态
		if ("2".equals(status)) {
			mediaProcess.setStatus("2");
			mediaProcess.setUrl(url);
			mediaProcess.setFinishDate(LocalDateTime.now());
			mediaProcessMapper.updateById(mediaProcess);
			//更新文件表中的url
			MediaFiles mediaFiles = mediaFilesMapper.selectById(fileId);
			mediaFiles.setUrl(url);
			mediaFilesMapper.updateById(mediaFiles);
			
		}
		//如果处理成功将任务添加到历史记录表
		MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
		BeanUtils.copyProperties(mediaProcess, mediaProcessHistory);
		mediaProcessHistoryMapper.insert(mediaProcessHistory);
		
		//如果处理成功将待处理表的记录删除
		mediaProcessMapper.deleteById(taskId);
		
		
	}
}
